home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / VideoToolbox / VideoToolboxSources / kbhit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-10  |  888 b   |  45 lines  |  [TEXT/KAHL]

  1. /*
  2. kbhit.c
  3. kbhit() returns 1 if a keypress awaits processing, 0 otherwise.
  4.  
  5. 6/13/90    dgp wrote kbhit, based on suggestion from Michael Kahl and an earlier version by
  6.         Evan Relkin.
  7. 2/16/93    dgp    added YesOrNo().
  8. 5/24/93 dgp Moved YesOrNo() to MultipleChoice.c
  9. 12/25/93 dgp cosmetic editing
  10. */
  11. #ifndef THINK_C
  12.     #error "Sorry, kbhit.c requires THINK C."
  13. #endif
  14. #include "VideoToolbox.h"
  15. #include <console.h>
  16.  
  17. int kbhit(void)
  18. {
  19. #if 1
  20.     // This works fine.
  21.     int c;
  22.  
  23.     c=getcharUnbuffered();
  24.     if(c==EOF)return 0;
  25.     ungetc(c,stdin);
  26.     return 1;
  27. #else
  28.     // This doesn't work. It always returns false. I don't understand why.
  29.     EventRecord event;
  30.     
  31.     return EventAvail(keyDown,&event);
  32. #endif
  33. }
  34.  
  35. int getcharUnbuffered(void)
  36. {
  37.     int c;
  38.  
  39.     csetmode(C_RAW,stdin);        /* unbuffered: no echo, no editing */
  40.     c=getchar();
  41.     csetmode(C_ECHO,stdin);        /* default mode: line-buffered, echo, full editing */
  42.     return c;
  43. }
  44.  
  45.